from socket import *
from threading import Thread

# lzb
def recv_data():
    while True:
        recv_data = udp_s.recvfrom(1024)
        recv_content = recv_data[0].decode('gbk')
        print(f"recv:{recv_content},from{recv_data[1]}")
        if recv_content == 'q':
            break

# lzb
def send_data():
    addr = ("127.0.0.1", 666)
    while True:
        data = input("请输入：")
        udp_s.sendto(data.encode("gbk"), addr)
        if data == 'q':
            break

# lzb
if __name__ == "__main__":
    udp_s = socket(AF_INET, SOCK_DGRAM)
    udp_s.bind(("127.0.0.1", 9000))
    th_1 = Thread(target=recv_data)
    th_2 = Thread(target=send_data)
    th_1.start()
    th_2.start()
    th_1.join()
    th_2.join()

